﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using InnovTech.Persistence;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace NHibernate.MsSqlCe.Test
{
    public class TestUtil
    {
        public static NHResource PersonMappingResource
        {
            get
            {
                return new NHResource("NHibernate.MsSqlCe.Test",
                    "NHibernate.MsSqlCe.Test.TestStuffs.Mappings.Person.hbm.xml");
            }
        }

        public static NHMsSqlCeDatabaseInfo CreateInfo()
        {
            return new NHMsSqlCeDatabaseInfo
            {
                DatabasePath = Path.Combine(Directory.GetCurrentDirectory(), "Test.sdf"),
                Password = "",
                Encrypted = false,
                MappingAction = new Action<Cfg.Configuration>(cfg =>
                {
                    cfg.AddAssembly(typeof(MsSqlCeSessionFactoryBuilderTest).Assembly);
                }),
            };
        }
    }

    public class TestBuilder : SessionFactoryBuilderBase<NHMsSqlCeDatabaseInfo>
    {

        public TestBuilder(NHMsSqlCeDatabaseInfo info)
            : base(info)
        {
        }


        protected override global::NHibernate.Cfg.Configuration GetConfiguration(NHMsSqlCeDatabaseInfo dbInfo)
        {
            Configuration config = new Configuration();
            config.AddProperties(dbInfo.GetConfigurationProperties());
            config.Properties.Add(NHibernate.Cfg.Environment.ConnectionString, dbInfo.GetConnectionString());
            dbInfo.GetMappingAction<Configuration>()(config);
            return config;
        }

        public void ExportTestSchema()
        {
            var config = BuildConfiguration();
            SchemaExport exporter = new SchemaExport(config);
            exporter.Create(false, true);

        }
    }
}
